//
//  DetailCollectionViewCell.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "DetailCollectionViewCell.h"
#import "DetailTableViewCell.h"
#import "InforDetail.h"
#import "GetConnection.h"
#import "FocusImages.h"
#import <MJRefresh.h>

@implementation DetailCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayData = [NSMutableArray array];
        self.arrayLoopsView = [NSMutableArray array];
        self.arrayTitle = [NSMutableArray array];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, self.frame.size.width, self.frame.size.height - 10) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.separatorStyle = 0;
    
    [self.contentView addSubview:self.tableView];
    
    [self setTableFooterView:self.tableView];
    
    [self addHeader];
    [self addFooter];
}
#pragma mark 上拉加载 下拉刷新
- (void)addHeader
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        self.nextPage = 1;
        [self getconnect:self.nextPage];
    }];
    [self.tableView.mj_header beginRefreshing];
}
- (void)addFooter
{
    self.tableView.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.isUpLoading = YES;
        self.nextPage += 1 ;
        [self getconnect:self.nextPage];
    }];
}
- (void)setTableFooterView:(UITableView *)tableView
{
    UIView *view= [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTableViewCell *cell = [DetailTableViewCell detailTableViewCellWith:tableView];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    InforDetail *infor = self.arrayData[indexPath.row];
    if (self.arrayData.count != 0) {
        cell.inforDetail = infor;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.customBlock([self.arrayData[indexPath.row] albumId]);
}
// set方法
- (void)setStrTitle:(NSString *)strTitle
{
    if (_strTitle != strTitle) {
        [_strTitle release];
        _strTitle = [strTitle copy];
        [self.tableView.mj_header beginRefreshing];
        [self getconnect:self.nextPage];
    }
}
- (void)getconnect:(NSInteger)nextPage
{
    // 拼接转码
    NSString *title = self.strTitle;
    title = [title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *str= [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=iPhone&pageId=%ld&pageSize=20&status=0&tagName=%@",self.strID,nextPage,title];
    [GetConnection startGETConnection:str parmaters:nil connectionBlock:^(id responseObject) {
        if (self.isUpLoading == NO) {
            [self.arrayData removeAllObjects];
        }
        NSDictionary *dic = responseObject;
        // 取出数组中的字典
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *temp in array) {
                InforDetail *inforDetail = [InforDetail inforDetailWithDictionary:temp];
            [self.arrayData addObject:inforDetail];
        }
        [self.tableView reloadData];
        self.tableView.separatorStyle = 0;
        // 停止刷新加载
        if (self.arrayData.count != 0) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            self.tableView.separatorStyle = 1;
        }
        // 没有更多数据
        if (array.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

@end
